<?php

class Unit extends CI_Model {

	function __construct() {
		parent::__construct();
	}

	function filter($data) {
		$msg = array();
		if (!array_key_exists('unit', $data)) $msg[] = '计费单元名称必须提供'; 
		if (!array_key_exists('service_id', $data)) $msg[] = '所属业务必须提供';
		if (!array_key_exists('user_id', $data)) $msg[] = '所属用户必须提供';
		if (!array_key_exists('charge_mode', $data)) $msg[] = '计费类型必须提供';
		if (!array_key_exists('fee_mode', $data)) $msg[] = '计费方法必须提供';
		
		foreach ($data as $k => $v) {
			switch ($k) {
				case 'unit':
					if (strlen(strval($v)) == 0) $msg[] = '计费单元名称不能为空';
					break;
				case 'charge_mode':
					if (intval($v) == 0) $msg[] = '计费类型数据错误';
					break;
				case 'fee_mode':
					if (intval($v) == 0) $msg[] = '计费方法数据错误';
					break;
				case 'user_id':
					if (intval($v) == 0) $msg[] = '所属用户数据错误';
					break;
				case 'service_id':
					if (intval($v) == 0) $msg[] = '所属业务数据错误';
					break;
			}
		} 
	
		$status = FALSE;
		if (empty($msg)) {
			$status = TRUE;
		}
		return array('status' => $status, 'msg' => $msg);
	}

	function add($cond) {
		$tmp = array('unit' => $cond['unit'], 'user_id' => $cond['user_id'], 'service_id' => $cond['service_id']);
		if ($this->exist($tmp)) return FALSE;
		$cond['status'] = 1;
		$cond['created_at'] = date('Y-m-d h:i:s', mktime());
		$cond['updated_at'] = date('Y-m-d h:i:s', mktime());
		$this->db->insert('unit', $cond);
		return $this->db->insert_id();
	}

	function exist($cond) {
		$this->db->where($cond);
		$query = $this->db->get('unit');
		if ($query->num_rows() >= 1) return TRUE;
		return FALSE;	
	}

	function cfind($data) {
		$cond['user_id'] = array_key_exists('user_id', $data) ? intval($data['user_id']) : 0;
		$cond['service_id'] = array_key_exists('service_id', $data) ? intval($data['service_id']) : 0;
		$cond['status'] = 1;
		if ($cond['user_id'] == 0) unset($cond['user_id']);
		if ($cond['service_id'] == 0) unset($cond['service_id']);
		$this->db->where($cond);
		$this->db->limit($data['per_page'], $data['page'] * $data['per_page']);
		$this->db->order_by('id', 'desc');
		$query = $this->db->get('unit');
		return $query->result_array();
	}

	function find($id) {
		$this->db->where('id', $id); 
		$query = $this->db->get('unit');
		return $query->result_array();
	}

	function total($data) {
		$cond['user_id'] = intval($data['user_id']);
		$cond['service_id'] = intval($data['service_id']);
		if ($cond['user_id'] == 0) unset($cond['user_id']);
		if ($cond['service_id'] == 0) unset($cond['service_id']);
		$cond['status'] = 1;
		$this->db->where($cond);
		$query = $this->db->get('unit');
		return $query->num_rows();
	}

	function del($id) {
		$data = array('status' => 0);
		$data['updated_at'] = date('Y-m-d h:i:s', mktime());
		$this->db->where('id', $id);
		$this->db->update('unit', $data); 
	}

	function user_unit($user_id) {
		$this->db->where('user_id', $user_id); 
		$this->db->where('status', 1); 
		$query = $this->db->get('unit');
		return $query->result_array();
	}

	function user_unit_array($user_id) {
		$data = $this->user_unit($user_id);
		$r = array();
		foreach ($data as $v) {
			$r[] = $v['id'];
		}
		return $r;
	}

	function service_unit($service_id, $user_id) {
		$service_id = intval($service_id);
		if ($service_id === 0) {
			// all unit choosen
		} elseif ($service_id == FALSE) {
			return array();
		} else {
			$this->db->where('service_id', $service_id);
		}
		if ($user_id !== 0) $this->db->where('user_id', $user_id);

		$this->db->where('status', 1); 
		$this->db->order_by('service_id');
		$query = $this->db->get('unit');
		return $query->result_array();
	}

	function unit_tree($service_id, $user_id) {
		$data = $this->service_unit($service_id, $user_id);
		$r = array();
		foreach ($data as $v) {
			$r[$v['service_id']][] = $v;
		}
		return $r;
	}

}
